<?php 

require_once "curl.php";

class loginObj {
	var $mycUrl;
	
	function __construct() {
		$this->mycUrl = new cUrl();
	}
	
	function getPage($url) {
		return $this->mycUrl->get($url);
	}

	function login($name, $passwd) {
		// STEP 1: get login _token
		$content = $this->mycUrl->get("www.erepublik.com/en");
	
		$pos = stripos($content, 'name="_token"');
		$pos += strlen('name="_token" value="');
		$token = substr($content, $pos, 32);
		echo "token: $token\r\n";
		
		// STEP 2: send POST request
		$fields = "_token=$token&citizen_email=" . urlencode($name) .
							"&citizen_password=" . urlencode($passwd) . "&commit=Login";
		
		$content = $this->mycUrl->post("www.erepublik.com/en/login", $fields);
		
		// STEP 3: check the result
		$result = $this->mycUrl->getinfo(CURLINFO_EFFECTIVE_URL);
		if ( stripos($result, "login") == FALSE ) {
			$result = TRUE;
		} else {
			$result = FALSE;
		}
	
		return $result;
	}

}

?>